<?php
$reservelistitemsStr = Yii::t('BWeSport', 'Reserve list items');
$addbasicinformatinStr = Yii::t('BWeSport', 'Add Basic Informatin');
$chooceStr = Yii::t('BWeSport', 'Chooce');
$basicinformatinStr = Yii::t('BWeSport', 'Basic Informatin');
$suppliersinformatinStr = Yii::t('BWeSport', 'Suppliers Informatin');
$nextstepStr = Yii::t('BWeSport', 'Next Step');
$saveStr = Yii::t('BWeSport', 'Save');
$model = new BDynamicFormModel();
$modelClass = get_class($model);
$screen = Blyn::app()->getUIManager()->getCurrentScreen();
$opUI = Blyn::app()->getActiveOperation()->getUIManager();
$url = $opUI->getAjaxLinkUrl('saveEvent', NULL, array('eventId' => $eventId, 'postModel' => $modelClass));
$updateId = $opUI->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function send<?php echo $eventId; ?>() {

        var formData = new FormData($("#post-form-2")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
                //$("#refreshIt").click();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>

<?php
$app = Blyn::app()->getActiveApp();
$typeList = $app->getEventTypeList('FullName');
$list = array('0' => $chooceStr);
$typeList = $list + $typeList;

/**
 * 定义field的名字的时候，一定按照以下规范来定义：
 * 1）下划线作为分隔符
 * 2）第一部分是类型，中间是class，然后是class里面的属性的名字，有时候还可以加一个后缀
 */
$fieldList = array();
$fieldList['eventType'] = $eventTypeField = 'eventType_eventTypeId';
$fieldList['eventName'] = $eventNameField = 'event_name';
$fieldList['eventStartTime'] = $startTimeField = 'event_startTime';
$fieldList['eventEndTime'] = $endTimeField = 'event_endTime';
$fieldList['eventDescription'] = $descriptionField = 'event_description';
$fieldList['eventImage'] = $imageField = 'image_media_image';

$model->addField($eventNameField);
$model->addField($startTimeField);
$model->addField($endTimeField);
$model->addField($descriptionField);
$model->addField($imageField);
$model->addField($eventTypeField);

if ($eventId > 0) {
    $event = new BWEvent($eventId);
    $eventType = $event->getType();
    if ($eventType)
        $model->$eventTypeField = $eventType->getId();
    $model->$eventNameField = $event->name;
    $model->$startTimeField = $event->startTime;
    $model->$endTimeField = $event->endTime;
    $model->$descriptionField = $event->description;
    $media = $event->getMediaManager()->getMedia('image');
    if ($media)
        $model->$imageField = $event->getMediaManager()->getMedia('image')->content;
}
?>
<?php
$form = $this->beginWidget('CActiveForm', array(
    "id" => "post-form-2",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
?>
<div class="modal-header">    
    <?php //echo $control->getAjaxLink(array('name' => $reservelistitemsStr, 'id' => 'showEventList')); ?>
    <h3><?php echo $addbasicinformatinStr; ?></h3>
</div>
<div class="modal-body" id="Event_Add_Body" style="display:block">
    <div class="tabbable tabs-right" style="margin-top: 15px;"> <!-- Only required for left/right tabs -->
        <ul class="nav nav-tabs" style="margin-bottom: 15px">
            <li class="active"><a href="#tab1" data-toggle="tab"><?php echo $basicinformatinStr; ?></a></li>      
            <li class=""><a href="#tab2" data-toggle="tab"><?php echo $suppliersinformatinStr; ?></a></li>   
        </ul>
        <div class="tab-content">
            <div class="tab-pane active" id="tab1" style="min-height: 270px;min-width: 500px">                
                <?php
                $control->renderView('event_add_info', array(
                    'form' => $form,
                    'eventId' => $eventId,
                    'model' => $model,
                    'fieldList' => $fieldList,
                    'typeList' => $typeList
                ));
                ?>  
            </div>  
            <div class="tab-pane " id="tab2" style="min-height: 380px">                
                <?php
                $control->renderView('vender_add_info', array(
                    'form' => $form,
                    'eventId' => $eventId,
                    'model' => $model,
                    'fieldList' => $fieldList,
                    'typeList' => $typeList
                ));
                ?>  
            </div>      
        </div>
    </div>

    <div class="modal-footer">
        <?php
        $htmlOptions = array('class' => 'btn');
        if ($eventId == 0) {
            $htmlOptions = array('class' => 'btn disabled');
        }
        echo $control->getAjaxLink(array('name' => $nextstepStr, 'id' => 'showMessage'), array('parentEventId' => $eventId,
            'messageView' => 'bookable_add',
            'htmlOptions' => $htmlOptions));
        ?>
        <?php
        echo '  ';
        ?>        
    </div>
    <?php $this->endWidget(); ?>
<button class="btn" data-dismiss="modal" aria-hidden="true" onclick="javascript:send<?php echo $eventId; ?>()">保存</button>
<button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>